package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.sky.constant.MessageConstant.DISH_BE_RELATED_BY_SETMEAL;
import static com.sky.constant.MessageConstant.DISH_ON_SALE;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @Transactional
    public void add(DishDTO dishDTO) {
        //1.创建菜品Dish对象
        Dish dish = new Dish();

        //2.给Dish对象赋值
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //3.调用mapper插入dish表菜品
        dishMapper.add(dish);
        //4.创建口味对象List
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //5.给口味对象赋值
        if(!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //6.调用mapper批量插入口味表数据
            dishFlavorMapper.addBatch(flavors);
        }


    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);//后绪步骤实现
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //1.判断传入的菜品id中有没有起售中的
        Integer dishNum = dishMapper.countStartDishNum(ids);
        //1.1有就抛出异常
        if (dishNum>0){
            throw new DeletionNotAllowedException(DISH_ON_SALE);
        }
        //2.判断菜品中是否有被套餐关联的
        Integer setmealNum = setmealDishMapper.countSetmealNum(ids);
        //2.1有就抛出异常
        if (setmealNum>0){
            throw new DeletionNotAllowedException(DISH_BE_RELATED_BY_SETMEAL);
        }
        //3删除菜品
        dishMapper.deleteBatch(ids);
        //4删除口味
        dishFlavorMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询菜品详情
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
//        //1.查询菜品数据
//        Dish dish = dishMapper.getById(id);
//        //2.查询口味数据
//        List<DishFlavor> dishFlavors =dishFlavorMapper.getByDishId(id);
//        //3.封装DishVO
//        DishVO dishVO = new DishVO();
//        BeanUtils.copyProperties(dish,dishVO);
//        dishVO.setFlavors(dishFlavors);
        return dishMapper.getDishVO(id);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    public void update(DishDTO dishDTO) {
        //1.创建dish对象
        Dish dish = new Dish();
        //2.赋值dish对象
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //3,调用mapper 修改菜品表
        dishMapper.update(dish);
        //4.删除口味
        dishFlavorMapper.deleteBatch(Arrays.asList(dishDTO.getId()));

        //5.获取List<Dishflavor> 口味集合
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //5.给口味对象赋值
        if(!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //6.调用mapper批量插入口味表数据
            dishFlavorMapper.addBatch(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId,String name) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .name(name)
                .build();

        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
